<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\admin\service\Menu as MenuService;
use app\admin\validate\Menu as ValidateMenu;
use app\lib\exception\Common as CommonException;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Response;

class Menu extends BaseController
{
    protected $code = 200;
    protected $message = "查询成功";

    public function menuAllList(): response
    {
        $request = $this->request;
        $request->filter(['trim']);
        $param = $request->param();
        $data = MenuService::menuAllList();
        return success($this->message, $data, $this->code);
    }

    public function menuList(): response
    {
        $request = $this->request;
        $request->filter(['trim']);
        $param = $request->param();
        $data = MenuService::menuList($param);
        return success($this->message, $data, $this->code);
    }

    public function menuDetail(): response
    {
        $request = $this->request;
        $request->filter(['trim']);
        $param = $request->param();
        try {
            validate(ValidateMenu::class)->scene('detail')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            throw new CommonException(['msg' => $e->getError()]);
        }

        $data = MenuService::menuDetail($param);
        return success($this->message, $data, $this->code);
    }

    public function menuAdd(): response{
        $request = $this->request;
        $request->filter(['trim']);
        $param = $request->param();
        try {
            validate(ValidateMenu::class)->scene('add')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            throw new CommonException(['msg' => $e->getError()]);
        }

        MenuService::menuAdd($param);
        $this->message = '操作成功';
        return success($this->message, [], $this->code);
    }
    public function menuEdit(): response{
        $request = $this->request;
        $request->filter(['trim']);
        $param = $request->param();
        try {
            validate(ValidateMenu::class)->scene('edit')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            throw new CommonException(['msg' => $e->getError()]);
        }

        $data = MenuService::menuEdit($param);
        $this->message = '操作成功';
        return success($this->message, [], $this->code);
    }

    public function menuDelete(): response{
        $request = $this->request;
        $request->filter(['trim']);
        $param = $request->param();
        try {
            validate(ValidateMenu::class)->scene('delete')->check($param);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            throw new CommonException(['msg' => $e->getError()]);
        }
        $data = MenuService::menuDelete($param);
        $this->message = '操作成功';
        return success($this->message, [], $this->code);
    }

}